
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "backbuffer.h"

BackBuffer::BackBuffer()
{
	Zero();
}

BackBuffer::~BackBuffer()
{
	Destroy();
}

void BackBuffer::Create(int width,int height)
{
	Destroy();

	HDC screendc = GetDC(0);
	if(!screendc)
		return;

	hBmp = CreateCompatibleBitmap(screendc,width,height);
	ReleaseDC(0,screendc);

	mDC = CreateCompatibleDC(0);
	if(!mDC)
	{
		Destroy();
		return;
	}

	hBmpOld = (HBITMAP)SelectObject(mDC,hBmp);

	nWidth = width;
	nHeight = height;
}

void BackBuffer::Destroy()
{
	if(mDC)
	{
		SelectObject(mDC,hBmpOld);
		DeleteDC(mDC);
	}
	if(hBmp)
		DeleteObject(hBmp);

	Zero();
}

void BackBuffer::Clear(COLORREF clr)
{
	if(!mDC)
		return;

	HBRUSH br = CreateSolidBrush(clr);
	RECT rc;
	SetRect(&rc,0,0,nWidth,nHeight);
	FillRect(mDC,&rc,br);

	DeleteObject(br);
}

void BackBuffer::Zero()
{
	mDC = 0;
	hBmp = 0;
	hBmpOld = 0;

	nWidth = 0;
	nHeight = 0;
}